<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class GoodsController extends BaseController {
	private $tbl;
	public function _initialize(){
		$this->tbl='goods';
		$this->model=D($this->tbl);
		parent::_initialize();
	}
	public function index(){
		parent::index($this->tbl);
	}
	public function	view(){
		$id=I('get.id');//获取id
	}
	public function show(){
		$id=I('get.id');		

		//所有信息
		$sql="select * from goods where id={$id}";
		//echo $sql;exit;
		$data1=M()->query($sql);
		$this->assign("good",$data1);
		
		 
		//分类信息
		$sql="select * from classfies where id='{$data1[0]['cid']}'";
		
		$data2=M()->query($sql);
		$this->assign("classname",$data2);
		
		//分数
		$sql="select avg(star) from scores where gid='{$data1[0]['id']}'";
		//echo $sql; exit;
		$data2=M()->query($sql);
		$ss=$data2['0']["avg(star)"];
		$this->assign("score",round($ss,2));
		//echo $ss;	exit;
		// 最新评论信息
		$sql="select * from scores where gid='{$data1[0]['id']}' order by id desc";
		$res=M()->query($sql);
		$this->assign("meslist",$res);
		$this->display();
	}
	public function mod(){//修改界面
		$sql="select * from classfies";
		$data1=M()->query($sql);
		$this->assign("idlist",$data1);
		 parent::mod($this->tbl,"id");
	}
	public function update(){//修改
		$id=I('post.id');
		$name=I('post.name');
		$Dao = M("scores");
		$result = $Dao->where("gid = '{$id}'")->setField('gname',$name);
		//echo $sql;exit;
		parent::update($this->tbl,"pic");
	}
	public function delete(){//删除
		parent::delete($this->tbl,"id","pic");
	}
	public function form(){//添加界面
		$sql="select * from classfies";
		$data1=M()->query($sql);
		$this->assign("idlist",$data1);
		parent::form($this->tbl);
	}
	public function add(){//添加
			$this->model=D("goods");
			$data=I('post.');
			$data['pic']=parent::uploadfile('pic');
			if($this->model->create($data)){
				$this->model->add();
				$position="index";
				$this->success('增加成功！',U($position));
			}else{
				echo $this->model->_sql();//最后一条执行的Sql
				echo "dskdskjdks";
				exit;
				$this->error($this->model->getError());
				$this->error("增加失败！",U($position));
			}
	}
	public function search(){
		parent::search($this->tbl,"name","name");
	}
}